package com.liuyang.demo.excel.util;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * @author yzw
 * @date 2019/08/20 10:38
 */
@Slf4j
public class ExcelUtil {

    /** 本地文件目录 */
    @Value("${local.file.path:/tmp}")
    private String localFilePath;

    public File generateExcel(List<Map<String, Object>> list, String[] keys, String[] columnNames) {
        OutputStream out = null;
        try {
            String path = localFilePath;
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String filePath = path + File.separator + getRandomFileName("xlsx");

            XSSFWorkbook wb = ExcelExportUtil.createWorkBook(list, keys, columnNames);
            out = new FileOutputStream(filePath);
            wb.write(out);
            out.flush();
            File file = new File(filePath);
            if (!file.exists()) {
                return null;
            }
            log.info("生成excel成功. filePath={}", filePath);
            return file;
        } catch (Exception e) {
            log.error("生成excel时异常", e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    log.error("关闭流时异常", e);
                }
            }
        }
        return null;
    }

    public File generateExcel(List<Object []> list, String[] columnNames, String sheetName) {
        OutputStream out = null;
        try {
            File dir = new File(localFilePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String filePath = localFilePath + File.separator + getRandomFileName("xlsx");

            XSSFWorkbook wb = ExcelExportUtil.createWorkBook(list, columnNames, sheetName);
            File file = new File(filePath);
            out = new FileOutputStream(file);
            wb.write(out);
            out.flush();
            if (!file.exists()) {
                return null;
            }
            log.info("生成excel成功. filePath={}", filePath);
            return file;
        } catch (Exception e) {
            log.error("生成excel时异常", e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    log.error("关闭流时异常", e);
                }
            }
        }
        return null;
    }

    public File generateExcel(List<Object []> list, String[] columnNames) {
        return generateExcel(list, columnNames, "sheet1");
    }

    private String getRandomFileName(String suffix) {
        return System.currentTimeMillis() + "" + RandomUtil.randomInt(100000) + "." + suffix;
    }
}
